package com.yhb.controller;

import com.yhb.conf.ResultCode;
import com.yhb.controller.form.BoundPosForm;
import com.yhb.controller.form.SimpleVerifyCodeForm;
import com.yhb.controller.form.TradeForm;
import com.yhb.controller.vo.ArrayWrapperVo;
import com.yhb.controller.vo.SimpleVerifyCodeTokenVo;
import com.yhb.model.PosBound;
import com.yhb.model.RapidCheat;
import com.yhb.model.User;
import com.yhb.service.MPosService;
import com.yhb.service.TradeService;
import com.yhb.third.leshua.LeShuaCheckBoundForm;
import com.yhb.utils.StringUtils;
import lombok.extern.log4j.Log4j;
import lombok.var;
import me.edzh.spring.bootstrap.annotations.CurrentUser;
import me.edzh.spring.bootstrap.annotations.EncryptedRequestBody;
import me.edzh.spring.bootstrap.annotations.NeedAuth;
import me.edzh.spring.bootstrap.controller.BaseController;
import me.edzh.spring.bootstrap.model.Response;
import me.edzh.spring.bootstrap.utils.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.stream.Collectors;

@Log4j
@RestController
@RequestMapping("/trade")
public class TradeController extends BaseController {

    @Autowired
    private TradeService tradeService;

    @Autowired
    private MPosService mPosService;

    @PostMapping("/commit")
    @NeedAuth
    public Response<SimpleVerifyCodeTokenVo> commitTrade(@EncryptedRequestBody TradeForm tradeForm, @CurrentUser User user, @RequestHeader(value = "ua", required = false) String ua, @RequestHeader(value = "OS", required = false)String osString, @RequestHeader(value = "VERSION", required = false) String versionString) {
        if (ua != null && ua.startsWith("ios/") && !ua.equals("ios/1.4")) {
            throw new ServiceException(ResultCode.ERROR_TOAST_ERR, "请升级最新版app使用");
        }

        if (osString != null && osString.startsWith("android/") && !"2.0.8".equals(versionString)) {
            throw new ServiceException(ResultCode.ERROR_TOAST_ERR, "请升级最新版app使用");
        }
        return Response.ok(tradeService.commitRapidCheat(tradeForm.getConsumeCardId(), tradeForm.getRemitCardId(), tradeForm.getAmount(), user, tradeForm.getProvince(), tradeForm.getCity(), tradeForm.getDistrict()));
    }

    @PostMapping("/execute")
    @NeedAuth
    public Response<RapidCheat> executeRapidCheat(@EncryptedRequestBody SimpleVerifyCodeForm tradeForm, @CurrentUser User user, @RequestHeader(value = "ua", required = false) String ua, @RequestHeader(value = "OS", required = false)String osString, @RequestHeader(value = "VERSION", required = false) String versionString) {
        if (ua != null && ua.startsWith("ios/") && !ua.equals("ios/1.4")) {
            throw new ServiceException(ResultCode.ERROR_TOAST_ERR, "请升级最新版app使用");
        }

        if (osString != null && osString.startsWith("android/") && !"2.0.8".equals(versionString)) {
            throw new ServiceException(ResultCode.ERROR_TOAST_ERR, "请升级最新版app使用");
        }
        return Response.ok(hideSensitiveInfos(tradeService.executeRapidCheat(tradeForm.getToken(), tradeForm.getVerifyCode(), user)));
    }

    @GetMapping("/{type}/{monthString}/list")
    @NeedAuth
    public Response<ArrayWrapperVo<RapidCheat>> queryTrade(@CurrentUser User user, @PathVariable("type") String type, @PathVariable("monthString") String monthString) {
        return Response.ok(new ArrayWrapperVo<>(
                tradeService.userList(user.getId(), RapidCheat.Type.valueOf(type), monthString)
                        .parallelStream()
                        .map(this::hideSensitiveInfos)
                        .collect(Collectors.toList())
        ));
    }

    @GetMapping("/rapid/{flowId}/query")
    @NeedAuth
    public Response<RapidCheat> queryRapidFlow(@PathVariable("flowId") String flowId, @CurrentUser User user) {
        return Response.ok(hideSensitiveInfos(tradeService.rapidFlow(flowId, user.getId())));
    }

    @PostMapping("/pos/bound")
    @NeedAuth
    public Response boundMPos(@EncryptedRequestBody BoundPosForm form, @CurrentUser User user) {

        mPosService.addBound(form.getChannelId(), form.getSn(), user.getId());
        return Response.ok();
    }

    public RapidCheat hideSensitiveInfos(RapidCheat rapidCheat) {
        if (rapidCheat == null) return null;

        rapidCheat.setConsumeCardNumber(StringUtils.getMaskCardNumber(rapidCheat.getConsumeCardNumber()));
        rapidCheat.setRemitCardNumber(StringUtils.getMaskCardNumber(rapidCheat.getRemitCardNumber()));

        return rapidCheat;
    }
}
